﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STRobot
{
    public partial class GuajipointAddWnd : Form
    {
        public int index_;
        private int pointIndex_;
        public Guajipoint result;
        public GuajipointAddWnd(int index = -1)
        {
            InitializeComponent();
            this.index_ = index;
            pointIndex_ = -1;
            if (index_ == -1)
            {
                result = new Guajipoint();
                result.points = new List<Point>();
            }
            else
            {
                result = Config.Get().guajipoint[index_];
            }
            textBox3.Text = result.showname;
            textBox1.Text = result.flushtime.ToString();
            textBox2.Text = result.idletime.ToString();
            textBox4.Text = result.timeout.ToString();
            show_points();
        }

        //添加挂机点坐标,可以变成编辑坐标
        private void button1_Click(object sender, EventArgs e)
        {
            var x = textBox5.Text.Length > 0 ? Convert.ToInt32(textBox5.Text) : 0;
            var y = textBox6.Text.Length > 0 ? Convert.ToInt32(textBox6.Text) : 0;
            var point = new Point(x, y);
            if (listBox1.SelectedIndex >= 0) //编辑保存
            {
                result.points[listBox1.SelectedIndex] = point;
            }
            else
            {
                if (result.points == null)
                {
                    result.points = new List<Point>();
                }
                result.points.Add(point);
                
            }
            show_points();
        }

        void show_points()
        {
            listBox1.Items.Clear();
            foreach (var point in result.points)
            {
                listBox1.Items.Add(point.GetString());
            }
        }
        //删除挂机点坐标
        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                result.points.RemoveAt(listBox1.SelectedIndex);
            }         
            show_points();
        }

        //保存关闭
        private void button3_Click(object sender, EventArgs e)
        {
            result.showname = textBox3.Text;
            result.idletime = Convert.ToInt32(textBox2.Text);
            result.flushtime = Convert.ToInt32(textBox1.Text);
            result.timeout = Convert.ToInt32(textBox4.Text);

            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0)
            {
                if (result.timeout == 0 && result.points.Count == 0)
                {
                    MessageBox.Show("需要添加坐标点");
                }
                else
                {

                    if (index_ > -1) //编辑
                    {
                        Config.Get().guajipoint[index_] = result;
                    }
                    else
                    {
                        Config.Get().guajipoint.Add(result);
                    }
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
            else
            {
                MessageBox.Show("需要完整填写内容");
            }
            
        }

        //编辑坐标点
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            button1.Text = "保存坐标";
            textBox5.Text = result.points[listBox1.SelectedIndex].x.ToString();
            textBox6.Text = result.points[listBox1.SelectedIndex].y.ToString();
        }
    }
}
